package com.avocado.bt;

import com.avocado.bt.impl.Tree;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zsh
 * @Date: 2023/10/17 11:00
 */
public class BTManager {

    private static final Map<String, Class<? extends IBTNode>> name2nodeClazz = new HashMap<>();

    private static final List<Tree> trees = new ArrayList<>();

    public static void addTree(Tree tree) {
        trees.add(tree);
    }

    public static List<Tree> getTrees() {
        return trees;
    }

    public static void register(String name, Class<? extends IBTNode> clazz) {
        name2nodeClazz.put(name, clazz);
    }

    public static boolean contain(String name) {
        return name2nodeClazz.containsKey(name);
    }

    public static IBTNode newNodeInstance(String name, String param) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<? extends IBTNode> aClass = name2nodeClazz.get(name);
        if(aClass == null) {
            throw new RuntimeException("不存在 " + name);
        }
        return aClass.getConstructor(String.class).newInstance(param);
    }


}
